using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

#if SQL2005
namespace SBPweb.Data.SQL2005
{

	/// <summary>
	/// This class describes a factory, wich is able to create parameterized sql commands to delete data
	/// from the database. The class uses stored procedure.
	/// </summary>
	public class DeleteByParameteredCommandFactory: IDeleteFactory<List<KeyValuePair<string, object>>>
	{

		#region Private fields

		private string mCommandName;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Initializes a new DeleteByParameteredCommandFactory
		/// </summary>
		/// <param name="storedProcCommandName">The name of the stored procedure</param>
		public DeleteByParameteredCommandFactory(string storedProcCommandName)
		{
			mCommandName = storedProcCommandName;
		}

		#endregion

		#region IDeleteFactory<List<KeyValuePair<string, object>>> Members

		/// <summary>
		/// This method constructs the delete sql command
		/// </summary>
		/// <param name="db">SqlDatabase db</param>
		/// <param name="parameters">The parameters of the stored procedure</param>
		/// <returns>SqlCommand</returns>
		public SqlCommand ConstructDeleteCommand(SqlDatabase db, List<KeyValuePair<string, object>> parameters)
		{
			SqlCommand command = db.GetStoredProcCommand(mCommandName);

			string format = "@{0}";

			foreach (KeyValuePair<string, object> kvp in parameters)
			{
				db.AddInParameter(command, string.Format(format, kvp.Key), SqlDatabase.GetDbTypeByType(kvp.Value.GetType()), kvp.Value);
			}

			return command;
		}

		#endregion

		#region IDbToBusinessEntityNameMapper Members

		/// <summary>
		/// This method maps the stored procedure parameter names to entity property names
		/// </summary>
		/// <param name="dbParameter">stored procedure parameter name</param>
		/// <returns>entity property name</returns>
		public string MapDbParameterToBusinessEntityProperty(string dbParameter)
		{
			return dbParameter;
		}

		#endregion
	}
}
#endif